package org.bugeden.netdisk.transfer

import org.apache.commons.codec.digest.DigestUtils
import java.util.Date

/**
 * Created by IntelliJ IDEA.
 * User: gzhang
 * Date: 2010-6-20
 * Time: 0:03:14
 * To change this template use File | Settings | File Templates.
 */

class Block private(var bid: String, var content: Array[Byte]) {

  private var _createdDate = new Date
  private var _desc = "{}"
  private var sid = ""

  def this(content: Array[Byte]) = this(DigestUtils.md5Hex(content), content)

  def this(bid: String) = this(bid, Array[Byte]())

  def data(): Array[Byte] = content

  def data(d: Array[Byte]): Block = {
    this.setData(d)
    this
  }

  def setData(d: Array[Byte]) = {
//    this.id(DigestUtils.md5Hex(d))
    this.sid = DigestUtils.sha256Hex(d)
    this.content = d
  }

  def desc(): String = this._desc
  def desc(d: String): this.type = {
    this.setDesc(d)
    this
  }
  def setDesc(d: String): Unit  = (this._desc = d)

  def id(): String = bid
  def id(id: String): this.type = {
    this.setId(id)
    this
  }
  def setId(id: String): Unit = (this.bid = id)

  def secId(): String = this.sid

  def createdDate(): Date = this._createdDate
  def createdDate(date: Date): this.type = {
    this.setCreatedDate(date)
    this
  }
  def setCreatedDate(date: Date): Unit = (this._createdDate = date)

}

object Block{
  def apply(id: String): Block = {
    val block = new Block(id)
    block.id(id)
  }

  def apply(data: Array[Byte]): Block = {
    val block = new Block(data)
    block
  }
}